package com.chendx.store.goods.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.goods.dto.SkuDto;
import com.chendx.model.goods.dto.SkuInfoDto;
import com.chendx.model.goods.pojo.Sku;
import com.chendx.model.order.pojo.OrderItem;
import com.chendx.store.api.goods.SkuControllerApi;
import com.chendx.store.goods.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chendx on 2022/4/18.
 */

@RestController
@RequestMapping("/api/sku")
public class SkuController implements SkuControllerApi {

    @Autowired
    private SkuService skuService;

    @GetMapping("/findBySpuId" )
    @Override
    public List<Sku> findBySpuId(Long spuId){
        List<Sku> list = skuService.list(Wrappers.<Sku>lambdaQuery().eq(Sku::getSpuId, spuId));
        return list;
    }

    @Override
    @PostMapping("/add")
    public ResponseResult add(@RequestBody SkuInfoDto dto) {
        return skuService.add(dto);
    }

    @GetMapping("/one/{id}")
    @Override
    public Sku findSkuById(@PathVariable("id") Long id){
        Sku sku = skuService.getById(id);
        return sku;
    }

    @Override
    @GetMapping("/all")
    public List<Sku> findAllSku() {
        List<Sku> list = skuService.list(Wrappers.<Sku>lambdaQuery().eq(Sku::getStatus, "1"));
        return list;
    }

    @Override
    @PostMapping("/decrCount")
    public ResponseResult decrCount(@RequestBody OrderItem orderItem) {

        return skuService.decrCount(orderItem);
    }

    @Override
    @PostMapping("/incrCount")
    public ResponseResult incrCount(@RequestBody OrderItem orderItem) {
        return skuService.incrCount(orderItem);
    }

    @Override
    @GetMapping("/getNoReadNotice")
    public ResponseResult getNoReadNotice() {
        return skuService.noReadNotice();
    }

    @Override
    @PostMapping("/updatePrice")
    public ResponseResult updatePrice(@RequestBody SkuDto dto) {
        dto.checkParam();
        return  skuService.updatePrice(dto);
    }

    @Override
    @GetMapping("/notice")
    public ResponseResult goodsNotice() {
        return skuService.notice();
    }

    @Override
    @GetMapping("read")
    public ResponseResult readNotice() {
        return skuService.read();
    }

    @Override
    @PostMapping("infos")
    public ResponseResult skuInfos(@RequestBody SkuDto dto) {
        return skuService.skuInfos(dto);
    }


}
